﻿// MoveLine.cpp: 实现文件
//

#include "pch.h"
#include "LiWi.h"
#include "afxdialogex.h"
#include "MoveLine.h"
#include "Data.h"
#include "liwishell.h"


// MoveLine 对话框

IMPLEMENT_DYNAMIC(MoveLine, CDialogEx)

MoveLine::MoveLine(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_MoveLine, pParent)
{

}

MoveLine::~MoveLine()
{
}

void MoveLine::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(MoveLine, CDialogEx)
	ON_BN_CLICKED(IDC_Start, &MoveLine::OnBnClickedStart)
	ON_WM_TIMER()
END_MESSAGE_MAP()


// MoveLine 消息处理程序

//启动按钮
void MoveLine::OnBnClickedStart()
{
	// TODO: 在此添加控件通知处理程序代码
	CString speed, accel, decel, xpoint, ypoint, zpoint, upoint;
	//轴参数
	GetDlgItem(IDC_Speed_1)->GetWindowTextW(speed);
	GetDlgItem(IDC_Accel_1)->GetWindowTextW(accel);
	GetDlgItem(IDC_Decel_1)->GetWindowTextW(decel);
	//运动点位
	GetDlgItem(IDC_XPoint)->GetWindowTextW(xpoint);
	GetDlgItem(IDC_YPoint)->GetWindowTextW(ypoint);
	GetDlgItem(IDC_ZPoint)->GetWindowTextW(zpoint);
	GetDlgItem(IDC_UPoint)->GetWindowTextW(upoint);
	float axisPoints[4] = { _ttof(xpoint),_ttof(ypoint),_ttof(zpoint),_ttof(upoint) };	//点位数组
	//设置轴参数
	PMC_Base(Data::handle, 4, Data::axisList);
	for (size_t i = 0; i < 4; i++)
	{
		int enable = 0;
		PMC_GetAxisEnable(Data::handle, i, &enable);
		if (enable == 0)
		{
			MessageBox(TEXT("请检查轴0-3使能情况！！！"));
			return;
		}
		PMC_SetAtype(Data::handle, i, 1);
		PMC_SetUnits(Data::handle, i, 100);
		PMC_SetSpeed(Data::handle, i, _ttof(speed));
		PMC_SetAccel(Data::handle, i, _ttof(accel));
		PMC_SetDecel(Data::handle, i, _ttof(decel));
	}

	//运行状态

	//GetDlgItem(IDC_Start)->EnableWindow(false);
	((CButton*)GetDlgItem(IDC_Mode0))->GetCheck();
	if (((CButton*)GetDlgItem(IDC_Mode0))->GetCheck())			//绝对
	{
		if (((CButton*)GetDlgItem(IDC_Type0))->GetCheck())	PMC_MoveAbs(Data::handle, 2, Data::axisList, axisPoints);
		else if (((CButton*)GetDlgItem(IDC_Type1))->GetCheck())	PMC_MoveAbs(Data::handle, 3, Data::axisList, axisPoints);
		else	PMC_MoveAbs(Data::handle, 4, Data::axisList, axisPoints);
	}
	else														//相对
	{
		if (((CButton*)GetDlgItem(IDC_Type0))->GetCheck())	PMC_Move(Data::handle, 2, Data::axisList, axisPoints);
		else if (((CButton*)GetDlgItem(IDC_Type1))->GetCheck())	PMC_Move(Data::handle, 3, Data::axisList, axisPoints);
		else	PMC_Move(Data::handle, 4, Data::axisList, axisPoints);
	}

}


BOOL MoveLine::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	((CButton*)GetDlgItem(IDC_Mode0))->SetCheck(true);
	((CButton*)GetDlgItem(IDC_Type0))->SetCheck(true);
	GetDlgItem(IDC_Speed_1)->SetWindowTextW(_T("100"));
	GetDlgItem(IDC_Accel_1)->SetWindowTextW(_T("1000"));
	GetDlgItem(IDC_Decel_1)->SetWindowTextW(_T("1000"));
	//运动点位
	GetDlgItem(IDC_XPoint)->SetWindowTextW(_T("100"));
	GetDlgItem(IDC_YPoint)->SetWindowTextW(_T("100"));
	GetDlgItem(IDC_ZPoint)->SetWindowTextW(_T("100"));
	GetDlgItem(IDC_UPoint)->SetWindowTextW(_T("100"));
	SetTimer(1, 200, NULL);

	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}


void MoveLine::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	//各窗口启动控件
	if (Data::CardIsConnect())
	{
		if (Data::runFlag())
		{
			GetDlgItem(IDC_Start)->EnableWindow(false);
		}
		else
		{
			GetDlgItem(IDC_Start)->EnableWindow(true);
		}
	}

	CDialogEx::OnTimer(nIDEvent);
}
